﻿using NodeCanvas.BehaviourTrees;
using System.Collections.Generic;
using UnityEngine;
using WorldMapBase.Tile.Pool;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.Pool
{
    public class LibertyBTreePool : ObjectPool<GameObject>
    {
        private int _initNum;
        protected GameObject _originObject;
        protected BehaviourTree _behaviourTree;
        private static Dictionary<string, BehaviourTree> behaviourTrees = new Dictionary<string, BehaviourTree>();

        public int BtId;

        public LibertyBTreePool(GraphPathHolder holder, int initNum, int capacity) :
            base(capacity,true,10,1,10)
        {
            var originObject = holder.graph;

#if UNITY_EDITOR
            _name = "街道Npc对象池: " + originObject.name;
#endif
            _initNum = initNum;
            _originObject = originObject;
            var behaviourTreeOwner = _originObject.GetComponent<BehaviourTreeOwner>();
            if (behaviourTreeOwner)
            {
                if (behaviourTrees.TryGetValue(holder.resPath, out var behaviourTree))
                {
                    if (behaviourTree == null)
                    {
                        _behaviourTree = behaviourTreeOwner.behaviour;
                        behaviourTrees[holder.resPath] = _behaviourTree;
                    }
                    else
                    {
                        _behaviourTree = behaviourTree;
                    }
                }
                else
                {
                    _behaviourTree = behaviourTreeOwner.behaviour;
                    behaviourTrees[holder.resPath] = _behaviourTree;
                }
                behaviourTreeOwner.SetActive(false);
                /*
                behaviourTreeOwner.behaviour = null;*/
            }
            BtId = holder.graphId;
        }

        public void PreInit()
        {
            OnInit(_initNum);
        }
        
        protected override GameObject ConstructItem()
        {
            if (_originObject == null)
            {
                UnityEngine.Debug.LogError("街道Npc对象池 _originObject 为空");
                return null;
            }
            
            var graph =  Object.Instantiate(_originObject,
                    Vector3.zero,
                    Quaternion.identity,
                    LibertyAIUtils.LibertyManager.NpcManager.transform);
            var behaviourTreeOwner = graph.GetComponent<BehaviourTreeOwner>();
            if (behaviourTreeOwner)
            {
                behaviourTreeOwner.behaviour = _behaviourTree;
                behaviourTreeOwner.Initialize();
            }
            graph.SetActive(false);
            return graph;
        }
        
        protected override void BeforeRecover(GameObject obj)
        {
            obj.transform.parent = LibertyAIUtils.LibertyManager.NpcManager.transform;
            obj.SetActive(false);
        }

        protected override void OnRemoveItem(GameObject obj)
        {
            GameObject.Destroy(obj);
        }

        public override void Destroy()
        {
            base.Destroy();
            _originObject = null;
            _behaviourTree = null;
            behaviourTrees.Clear();
        }
    }
}
